/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_29			*/
/*	Bloom:		MrUtl.cpp			*/
/********************************************************/

#include "MrUtl.h"

void MrLocalDay(char* pszDay)
{
	time_t ulTime;
	struct tm* pstTime = NULL;

	ulTime = time(NULL);
	pstTime = localtime(&ulTime);
	strftime(pszDay, MR_DAYTIME, "%Y-%m-%d", pstTime);

	return;
}

void MrLocalTime(char* pszTime)
{
	time_t ulTime;
	struct tm* pstTime = NULL;

	ulTime = time(NULL);
	pstTime = localtime(&ulTime);
	strftime(pszTime, MR_DAYTIME, "%H:%M:%S", pstTime);

	return;
}

void MrLocalDayTime(char* pszDayTime)
{
	time_t ulTime;
	struct tm* pstTime = NULL;

	ulTime = time(NULL);
	pstTime = localtime(&ulTime);
	strftime(pszDayTime, MR_DAYTIME, "%Y-%m-%d %H:%M:%S", pstTime);

	return;
}

char* MrLocalPath(char* pszEnv)
{
	return getenv(pszEnv);
}

void MrReset(void* pReset, ULONG ulSize)
{
	if (NULL != pReset) {
		memset(pReset, '\0', sizeof(UCHAR) * ulSize);
	}

	return;
}

void* MrMalloc(ULONG ulSize)
{
	void* pReturn = NULL;

	if (NULL != (pReturn = malloc(sizeof(UCHAR) * ulSize))) {
		memset(pReturn, '\0', sizeof(UCHAR) * ulSize);
	}
	else {
		MR_ERROR("%s\n", strerror(errno));
	}

	return pReturn;
}

void MrFree(void** ppFree)
{
	if (NULL != *ppFree) {
		free(*ppFree);
		*ppFree = NULL;
	}

	return;
}

void MrFork()
{
	switch (fork()) {
		case MR_SUCCESS:
			umask(0);
			setsid();
			chdir("/");
			break;
		case MR_FAILURE:
			MR_ERROR("%s\n", strerror(errno));
			break;
		default:
			sleep(1);
			exit(MR_SUCCESS);
			break;
	}

	return;
}

void MrPacketDisplay(UCHAR* puszPacket, int iLength)
{
	int iCount = 0;

	MR_PRINT("Packet[%d]:", iLength);
	for (; iCount < iLength; iCount ++) {
		if (0 == iCount % sizeof(int)) {
			if (0 == iCount % (sizeof(int) * 2)) {
				MR_PRINT("\n");
			}
			else {
				MR_PRINT(" ");
			}
		}
		MR_PRINT("0x%02x ", *(puszPacket + iCount));
	}
	MR_PRINT("\n");

	return;
}
